package com.zjzywl.smoke.controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zjzywl.smoke.authenticate.AuthUser;
import com.zjzywl.smoke.repository.AlarmRepository;
import com.zjzywl.smoke.repository.YoyRepository;
import com.zjzywl.smoke.util.DataUill;
import com.zjzywl.smoke.util.WebResult;
import com.zjzywl.smoke.entity.Yoy;

@RestController
@RequestMapping(value = "/bigData/species")
public class SpeciesController extends BaseController {
	@Autowired
	private YoyRepository yoyrepository;

	@Autowired
	private AlarmRepository alarmRepository;

	@Autowired
	private AlarmRepository repository;
	DataUill dataUtil = new DataUill();

	// 按照时间段统计设备的三色报警数量
	@RequestMapping(value = "/device/count")

	public WebResult getSpeciesAlarmCountEx(@PathParam("beginTime") String beginTime,
			@PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
			@PathParam("fireCount") String fireCount) throws ParseException {
		String userId = this.getUserId();
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date bTime = date.parse(beginTime);
		Date eTime = date.parse(endTime);
		long time = (eTime.getTime() - bTime.getTime());
		List alarms = repository.getSpeciesAlarmCountByTimeEx(beginTime, endTime, alarmTypeCode, fireCount, userId);
		if (time <= 604800000) {
			calculateYOY(alarmTypeCode, fireCount, userId);
		} else if (time > 604800000 && time < 2592000000l) {
			calculateYOYByMonth(alarmTypeCode, fireCount, userId);
		} else {
			calculateYOYByQuarter(alarmTypeCode, fireCount, userId);
		}
		return WebResult.OK(alarms);
	}

	// 存入周同比时间
	private void calculateYOY(String alarmTypeCode, String fireCount, String userId) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		Date currentTime = dataUtil.getCurrentTime();// 当前时间
		System.out.println("当前时间:" + format.format(currentTime));
		Date beforeCurrentTime = dataUtil.getBeforeWeekCurrentTime();// 上周当前时间
		System.out.println("上周的当前时间:" + format.format(beforeCurrentTime));

		Date CurrentWeekTime = dataUtil.getBeginDayOfWeek();
		Date beforeCurrentWeekTime = dataUtil.getBeforeWeekBeginDayOfWeek();
		System.out.println("上周一时间:" + format.format(beforeCurrentWeekTime));
		List secondCount = repository.getSpeciesAlarmCountByTimeEx(format.format(CurrentWeekTime),
				format.format(currentTime), alarmTypeCode, fireCount, userId);

		List firstCount = repository.getSpeciesAlarmCountByTimeEx(format.format(beforeCurrentWeekTime),
				format.format(beforeCurrentTime), alarmTypeCode, fireCount, userId);
		Yoy yoy = new Yoy();
		yoy.setSecondCount(secondCount.size());
		yoy.setFirstCount(firstCount.size());
		DecimalFormat df = new DecimalFormat("#.00");
		if (secondCount.size() == 0) {
			yoy.setValue(Double.parseDouble(df.format(-firstCount.size())));
		} else if (firstCount.size() == 0) {
			yoy.setValue(Double.parseDouble(df.format(secondCount.size())));
		} else {

			yoy.setValue(Double
					.parseDouble(df.format((secondCount.size() - firstCount.size()) / (double) firstCount.size())));
		}
		if (fireCount.equals("10")) {
			yoy.setType("一周高危场所同比");
			yoyrepository.deleteByType("一周高危场所同比");
		} else {
			yoy.setType("一周中危场所同比");
			yoyrepository.deleteByType("一周中危场所同比");
		}
		yoyrepository.save(yoy);
	}

	// 存入月同比时间
	private void calculateYOYByMonth(String alarmTypeCode, String fireCount, String userId) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		Date currentTime = dataUtil.getCurrentTime();// 当前时间
		System.out.println("当前时间:" + format.format(currentTime));

		Date beforeCurrentTime = dataUtil.getBeforeMonthCurrentTime();// 上月当前时间
		System.out.println("上月的当前时间:" + format.format(beforeCurrentTime));

		Date CurrentMonthTime = dataUtil.getTimesMonthmorning();
		System.out.println("本月一号时间:" + format.format(CurrentMonthTime));
		Date beforeCurrentMonthTime = dataUtil.getBeginDayOfLastMonth();
		System.out.println("上月一号时间:" + format.format(beforeCurrentMonthTime));
		List secondCount = repository.getSpeciesAlarmCountByTimeEx(format.format(CurrentMonthTime),
				format.format(currentTime), alarmTypeCode, fireCount, userId);
		List firstCount = repository.getSpeciesAlarmCountByTimeEx(format.format(beforeCurrentMonthTime),
				format.format(beforeCurrentTime), alarmTypeCode, fireCount, userId);
		Yoy yoy = new Yoy();
		yoy.setSecondCount(secondCount.size());
		yoy.setFirstCount(firstCount.size());
		DecimalFormat df = new DecimalFormat("#.00");
		if (secondCount.size() == 0) {
			yoy.setValue(Double.parseDouble(df.format(-firstCount.size())));
		} else if (firstCount.size() == 0) {
			yoy.setValue(Double.parseDouble(df.format(secondCount.size())));
		} else {

			yoy.setValue(Double
					.parseDouble(df.format((secondCount.size() - firstCount.size()) / (double) firstCount.size())));
		}
		if (fireCount.equals("10")) {
			yoy.setType("一月高危场所同比");
			yoyrepository.deleteByType("一月高危场所同比");
		} else {
			yoy.setType("一月中危场所同比");
			yoyrepository.deleteByType("一月中危场所同比");
		}
		yoyrepository.save(yoy);
	}

	// 存入季度同比时间
	private void calculateYOYByQuarter(String alarmTypeCode, String fireCount, String userId) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		Date currentTime = dataUtil.getCurrentTime();// 当前时间
		System.out.println("当前时间:" + format.format(currentTime));
		Date beforeCurrentTime = dataUtil.getBeforeQuarterCurrentTime();// 上季度当前时间
		System.out.println("上季度的当前时间:" + format.format(beforeCurrentTime));
		Date CurrentQuarterTime = dataUtil.getCurrentQuarterStartTime();
		System.out.println("当季度的第一天:" + format.format(CurrentQuarterTime));
		Date beforeCurrentQuarterTime = dataUtil.getBeforeQuarterStartTime();
		System.out.println("上季度的第一天:" + format.format(beforeCurrentQuarterTime));
		List secondCount = repository.getSpeciesAlarmCountByTimeEx(format.format(CurrentQuarterTime),
				format.format(currentTime), alarmTypeCode, fireCount, userId);
		List firstCount = repository.getSpeciesAlarmCountByTimeEx(format.format(beforeCurrentQuarterTime),
				format.format(beforeCurrentTime), alarmTypeCode, fireCount, userId);
		Yoy yoy = new Yoy();
		yoy.setSecondCount(secondCount.size());
		yoy.setFirstCount(firstCount.size());
		DecimalFormat df = new DecimalFormat("#.00");
		if (secondCount.size() == 0) {
			yoy.setValue(Double.parseDouble(df.format(-firstCount.size())));
		} else if (firstCount.size() == 0) {
			yoy.setValue(Double.parseDouble(df.format(secondCount.size())));
		} else {

			yoy.setValue(Double
					.parseDouble(df.format((secondCount.size() - firstCount.size()) / (double) firstCount.size())));
		}
		if (fireCount.equals("10")) {
			yoy.setType("一季度高危场所同比");
			yoyrepository.deleteByType("一季度高危场所同比");
		} else {
			yoy.setType("一季度中危场所同比");
			yoyrepository.deleteByType("一季度中危场所同比");
		}
		yoyrepository.save(yoy);
	}

	// 返回第几个月份，不是几月
	// 季度一年四季， 第一季度：2月-4月， 第二季度：5月-7月， 第三季度：8月-10月， 第四季度：11月-1月
	public static int getQuarterInMonth(int month, boolean isQuarterStart) {
		int months[] = { 1, 4, 7, 10 };
		if (!isQuarterStart) {
			months = new int[] { 3, 6, 9, 12 };
		}
		if (month >= 2 && month <= 4)
			return months[0];
		else if (month >= 5 && month <= 7)
			return months[1];
		else if (month >= 8 && month <= 10)
			return months[2];
		else
			return months[3];
	}

	// 查询当前用户的所有报警原因
	@RequestMapping(value = "/alarmReason/count")

	public WebResult getAlarmReasonCount() throws ParseException {
		AuthUser userId = this.getLoginUser();
		String disposePeople = userId.getPeople().getName();
		List alarmReasonByuser = alarmRepository.getAlarmReasonByuser(disposePeople);
		return WebResult.OK(alarmReasonByuser);
	}

	// 查询当前用户的所有报警场所
	@RequestMapping(value = "/fixedPlace/count")

	public WebResult getFixedPlaceCount() throws ParseException {
		String userId = this.getUserId();
		List alarmReasonByuser = alarmRepository.getFixedPlaceByuser(userId);
		return WebResult.OK(alarmReasonByuser);
	}
}
